package com.mm.controller.system;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.DynamicParameter;
import com.github.xiaoymin.knife4j.annotations.DynamicResponseParameters;
import com.mm.entity.MmUser;
import com.mm.enums.ResultCodeEnum;
import com.mm.exception.BusinessException;
import com.mm.service.impl.MmUserServiceImpl;
import com.mm.utils.ApiResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author wdx
 * @since 2020-10-27
 */
@Api(tags = "用户API")
@Validated
@RestController
@RequestMapping("/mm-user")
public class MmUserController {

    @Autowired
    MmUserServiceImpl mmUserService;

    /*@GetMapping("/a")
    @ApiOperationSupport(
            responses = @DynamicResponseParameters(properties = {
                    @DynamicParameter(value = "订单",name = "sssss",dataTypeClass = MmUser.class)
            })
    )
    public Map<String, Object> aa() {
        Map map = new HashMap();
        List<MmUser> list = mmUserService.list();
        map.put("sssss", list);
        List<String> a = new ArrayList<>();
        a.add("1321");
        a.add("1321");
        a.add("1321");
        a.add("1321");
        map.put("a", a);
        return map;
    }*/

    @ApiOperation(value = "分页列表")
    @GetMapping("/{pageNo}/{pageSize}")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "pageNo", value = "当前页", defaultValue = "1", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "一页条数", defaultValue = "10", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "query", value = "查询条件", dataType = "string", paramType = "query")
    })
    public ApiResult<IPage<MmUser>> page(@NotNull(message = "当前页不能为空") @PathVariable("pageNo") Integer pageNo, @NotNull(message = "页条数不能为空") @PathVariable("pageSize") Integer pageSize, String query) {
        IPage<MmUser> page = mmUserService.page(new Page<>(pageNo, pageSize), query().like(StringUtils.isNotBlank(query), MmUser::getUsername, query).orderByDesc(MmUser::getId));
        return ApiResult.success(page);
    }

    @ApiOperation(value = "保存")
    @PostMapping
    public ApiResult save(@RequestBody MmUser mUser) {
        boolean save = mmUserService.save(mUser);
        if (!save) {
            throw new BusinessException(ResultCodeEnum.ADD);
        }
        return ApiResult.success();
    }

    @ApiOperation(value = "详情")
    @GetMapping("/{id}")
    @ApiImplicitParam(name = "id", value = "主键ID", dataType = "int", paramType = "query")
    public ApiResult<MmUser> detail(@NotNull(message = "ID不能为空") @PathVariable Integer id) {
        MmUser mUser = mmUserService.getById(id);
        if (mUser == null) {
            throw new BusinessException(ResultCodeEnum.NO_DATA);
        }
        return ApiResult.success(mUser);
    }


    @ApiOperation(value = "修改")
    @PutMapping
    public ApiResult update(@RequestBody MmUser mUser) {
        boolean update = mmUserService.updateById(mUser);
        if (!update) {
            throw new BusinessException(ResultCodeEnum.UPDATE);
        }
        return ApiResult.success();
    }

    @ApiOperation(value = "删除")
    @ApiImplicitParam(name = "id", value = "主键ID", dataType = "int", paramType = "query")
    @DeleteMapping("/{id}")
    public ApiResult del(@NotNull(message = "ID不能为空") @PathVariable Integer id) {
        boolean del = mmUserService.removeById(id);
        if (!del) {
            throw new BusinessException(ResultCodeEnum.DELETE);
        }
        return ApiResult.success();
    }

    /**
     * 查询条件简化
     *
     * @return
     */
    public LambdaQueryWrapper<MmUser> query() {
        return new LambdaQueryWrapper<MmUser>();
    }

}
